#include "logManager.h"
#include "commdef.h"
#include "buffer.h"
#include "tlib/tlib.h"
#include "helper.h"
#include "resource.h"
#include "shareManager.h"
#include "cfg.h"

LogManager::LogManager( Cfg* cfg )
: _cfg( cfg ){}

bool LogManager::WriteSendStart()
{
	return WriteStart();
}

bool LogManager::WriteSendHead( LPCSTR head )
{
	char buf[MAX_LISTBUF] = { 0 };
	
	wsprintf( buf, "To: %s\r\n", head );
	return Write( buf );
}

bool LogManager::WriteSendMsg( LPCSTR msg, ULONG command, ShareInfo* shareInfo /* = NULL */ )
{
	return WriteMsg( msg, command, shareInfo );
}

bool LogManager::WriteRecvMsg( MsgBuf* msg, THosts* hosts, ShareInfo* shareInfo /* = NULL */ )
{
	if( msg->command & IPMSG_NOLOGOPT )
		return false;
	WriteStart();
	char buf[MAX_PATH_U8] = "From: ";
	MakeListString( _cfg, &msg->hostSub, hosts, buf + strlen( buf) );
	strcat_s( buf, MAX_PATH_U8 - strlen( buf ), "\r\n" );
	Write( buf );
	return WriteMsg( msg->msgBuf, msg->command, shareInfo );
}

bool LogManager::WriteStart()
{
	return Write( "==========================================\r\n" );
}

bool LogManager::WriteMsg( LPCSTR msg, ULONG command, ShareInfo* shareInfo /* = NULL */ )
{
#define BUFLEN (MAX_BUF*2 - strlen( buf ))

	char buf[MAX_BUF*2] = " at ";
	strcat_s( buf, BUFLEN, Ctime() );
	strcat_s( buf, BUFLEN, " " );
	if( command & IPMSG_BROADCASTOPT )
		strcat_s( buf, BUFLEN, GetLoadStrU8(IDS_BROADCASTLOG) );
	if( command & IPMSG_AUTORETOPT )
		strcat_s( buf, BUFLEN, GetLoadStrU8(IDS_AUTORETLOG) );
	if( command & IPMSG_MULTICASTOPT )
		strcat_s( buf, BUFLEN, GetLoadStrU8(IDS_MULTICASTLOG) );
	if( command & IPMSG_ENCRYPTOPT )
		strcat_s( buf, BUFLEN, GetLoadStrU8(IDS_ENCRYPT) );
	if( command & IPMSG_SECRETOPT )
		if( command & IPMSG_PASSWORDOPT )
			strcat_s( buf, BUFLEN, GetLoadStrU8(IDS_PASSWDLOG) );
		else
			strcat_s( buf, BUFLEN, GetLoadStrU8(IDS_SECRETLOG) );

	if( shareInfo && ( command & IPMSG_FILEATTACHOPT ) )
	{
		strcat_s( buf, BUFLEN, "\r\n" );
		strcat_s( buf, BUFLEN, GetLoadStrU8(IDS_FILEATTACH) );
		strcat_s( buf, BUFLEN, " " );
		char fileName[MAX_PATH_U8] = { 0 };
		char* ptr = buf + strlen( buf );
		for( int cnt = 0; cnt < shareInfo->fileCnt && ptr - buf < sizeof( buf ) - MAX_PATH_U8; ++cnt )
		{
			ForcePathToFileName( shareInfo->fileInfos[cnt]->GetFileName(), fileName );
			ptr += wsprintf( ptr, "%s%s", fileName, cnt + 1 == shareInfo->fileCnt ? "" : ", " );
		}
	}
	strcat_s( buf, BUFLEN, "\r\n------------------------------------\r\n" );

	return ( Write( buf ) && Write( msg ) && Write( "\r\n\r\n" ) );
}

bool LogManager::Write( LPCSTR str )
{
	bool ret = false;

	if( !_cfg->logCheck || _cfg->logFile == 0 )
		return true;
	HANDLE hFile = CreateFileU8( _cfg->logFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
		NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
	if( hFile == INVALID_HANDLE_VALUE )
		return false;
	::SetFilePointer( hFile, 0, 0, FILE_END );
	str = _cfg->logUTF8 ? str : U8toA( str );
	DWORD size = 0;
	ret = ::WriteFile( hFile, str, strlen(str), &size, NULL ) ? true : false;
	::CloseHandle( hFile );
	return ret;
}
